package com.shangcheng.server.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangcheng.model.domain.CmsSubject;
import com.shangcheng.model.domain.CmsSubjectCategory;
import com.shangcheng.model.dto.CmsSubjectAndCategoryName;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.mapper.CmsSubjectCategoryMapper;
import com.shangcheng.server.mapper.CmsSubjectMapper;
import com.shangcheng.server.service.CmsSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2022/5/12 0012 zihao
 */
@Service
@Slf4j
public class CmsSubjectServiceImpl extends ServiceImpl<CmsSubjectMapper, CmsSubject> implements CmsSubjectService {

    @Autowired
    private CmsSubjectCategoryMapper cmsSubjectCategoryMapper;

    @Override
    //根据专题名称分页获取商品专题
    public PageResult lists(String keyword, Integer pageNum, Integer pageSize) {
        //将专题分类表的数据提取出来
        List<CmsSubjectCategory> cmsSubjectCategories = cmsSubjectCategoryMapper.selectList(null);
        Map<Long, String> map = new HashMap<Long, String>();
        for (CmsSubjectCategory cmsSubjectCategory : cmsSubjectCategories) {
            Long id = cmsSubjectCategory.getId();
            String name = cmsSubjectCategory.getName();
            map.put(id, name);
        }

        //查到包含keyword专题表的内容
        Page<CmsSubject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CmsSubject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!ObjectUtils.isEmpty(keyword),CmsSubject::getTitle, keyword);
        //查到所有包含且分页的对象,但是缺少一个数值
        Page<CmsSubject> p = this.page(page, lambdaQueryWrapper);
        Page<CmsSubjectAndCategoryName> pp = new Page<>();
        //从大拷贝到小
        BeanUtils.copyProperties(p, pp, "records");
        List<CmsSubjectAndCategoryName> CmsSubjectAndCategoryNameList = new ArrayList<>();
        List<CmsSubject> records1 = p.getRecords();
        for (CmsSubject cmsSubject : records1) {
            CmsSubjectAndCategoryName cmsSubjectAndCategoryName = new CmsSubjectAndCategoryName();
            BeanUtils.copyProperties(cmsSubject, cmsSubjectAndCategoryName);
            cmsSubjectAndCategoryName.setName(map.get(cmsSubject.getCategoryId()));
            CmsSubjectAndCategoryNameList.add(cmsSubjectAndCategoryName);
        }
        pp.setRecords(CmsSubjectAndCategoryNameList);

        //将分页对象拆分进PageResult
        PageResult pageResult = new PageResult(Convert.toInt(pp.getCurrent()), Convert.toInt(pp.getSize()), Convert.toInt(pp.getTotal()), pp.getRecords());
        return pageResult;
    }



    //获取全部商品专题
    @Override
    public List<CmsSubjectAndCategoryName> listAll() {
        //将专题分类表的数据提取出来
        List<CmsSubjectCategory> cmsSubjectCategories = cmsSubjectCategoryMapper.selectList(null);
        Map<Long, String> map = new HashMap<Long, String>();
        for (CmsSubjectCategory cmsSubjectCategory : cmsSubjectCategories) {
            Long id = cmsSubjectCategory.getId();
            String name = cmsSubjectCategory.getName();
            map.put(id, name);
        }

        List<CmsSubjectAndCategoryName> d = new ArrayList<>();
        List<CmsSubject> x = this.list(null);
        for (CmsSubject xx : x) {

            CmsSubjectAndCategoryName cmsSubjectAndCategoryName = new CmsSubjectAndCategoryName();
            BeanUtils.copyProperties(xx,cmsSubjectAndCategoryName);
            cmsSubjectAndCategoryName.setName(map.get(xx.getCategoryId()));
            d.add(cmsSubjectAndCategoryName);
        }
        return d;
    }
}
